﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace B12_Ex01_Guy_066473471_Erez_021616735.userInterfaceForWindowsInWinForm
{
    
    public class AppSetting
    {
        protected const String k_NameFile = "Setting.xml";

        public String AccessToken { get; set; }
        public String UserName { get; set; }
        public bool AutoLogIn { get; set; }

        private static AppSetting instance;
        
        /// <summary>
        /// singlton 
        /// </summary>
        private AppSetting() { }

        public static AppSetting Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new AppSetting();
                }
                return instance;
            }
        }

        public void SaveToFile()
        {
            using (FileStream Stream = new FileStream(k_NameFile, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(AppSetting));
                serializer.Serialize(Stream, this);
            }
        }

        public static AppSetting loadFromFile()
        {
            if (instance == null)
            {
                using (FileStream Stream = new FileStream(k_NameFile, FileMode.OpenOrCreate))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(AppSetting));
                    instance = (AppSetting)serializer.Deserialize(Stream);
                }
            }
            return instance;
        }
    }
}
